﻿
//附加管理

var m_Attachment_BillType;
var m_Attachment_BillId;
var m_Attachment_CaseId;
var m_Attachments;

var m_Attachment_User;//当期登录用户



$(function () {

    var args = getUrlArgs();

    m_Attachment_CaseId = args["CaseId"];
    m_Attachment_BillType = args["billType"];
    m_Attachment_BillId = args["billId"];

    m_Attachment_User = gGetLoginUser();//获取当前登录用户

    var button = $('#btnUploadFile'), interval;
    var fileType = "all", fileNum = "one";

    new AjaxUpload(button, {
        action: '/AttachmentUpload.ashx',
        name: 'userfile',
        onSubmit: function (file, ext) {

            var text = "文件上传中";
            //$('#maskAttachDiv').mask(text);
            this.setData({'CaseId':m_Attachment_CaseId,'billType': m_Attachment_BillType, 'billId': m_Attachment_BillId, 'deptId': m_Attachment_User.Department.Id, 'userId': m_Attachment_User.Id });
            if (fileNum == 'one')
                this.disable();
            interval = window.setInterval(function () {
                if (text.length < 20) {
                    text += ".";
                } else {
                    text = "文件上传中";
                }
            }, 200);


        },
        onComplete: function (file, response) {//上传成功的函数；response代表服务器返回的数据  
            //清楚按钮的状态  
            //button.text('文件上传');
            window.clearInterval(interval);
            this.enable();
            loadAttachFiles();
            $.messager.alert('提示', response);

        }
    });

    $('#attachFileGrid').datagrid({
        fitColumns: true,
        rownumbers: true,
        resizable: true,
        pagination: false,
        nowrap: false,
        height: 350,
        //idField: 'Id',
        singleSelect: true,
        toolbar: '#attachFileToolbar',
        columns: [[
            //{ title: 'Id', field: 'Id', align: 'center', width: 10, hidden: true },
            { title: '分类', field: 'BillType', align: 'left', width: 90,formatter:fmtbillType},
            { title: '文件名', field: 'Name', align: 'left', width: 200, formatter: urlFormat },
            { title: '大小', field: 'Size', align: 'right', width: 100, formatter: sizeFormat },
            { title: '上传日期', field: 'SubmitTime', align: 'right', width: 80, formatter: dateFormat },
            //{ title: '上传部门', field: 'Department', align: 'left', width: 80, formatter: fmtDeptName },
            { title: '上传人', field: 'Submitter', align: 'left', width: 80, formatter: fmtSubmitterName }
            

        ]]
    });

    loadAttachFiles();

});

function fmtbillType(value, rowData, index) {
    return gGetBillTypeName(value);
}

function fmtSubmitterName(value, rowData, index) {
    return value.Name;
}

function fmtDeptName(value, rowData, index) {
    return gFormatDeptName(value.Name);
}


function urlFormat(value, rowData, index) {
    //var s = '<a style="cursor:pointer" onclick="fileDownload('" + rowData.Url + "','" + value + "');'>" + value + "</a>";
    return '<a style="cursor:pointer" href="/Attachment/FileDownLoad?url=' + rowData.Url + '&name=' + value + '">' + value + '</a>';
};


function sizeFormat(value, rowData, index) {
    if (value) {
        var v =value / 1024;
        var s =v.toFixed(0);
        return s + " KB";
    }
    
}

function dateFormat(value, rowData, index) {
    var t = eval('new ' + eval(value).source);
    return t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + (t.getDate());
}

function fileDownload(url, name) {
    $.ajax({
        url: "/AttachFile/FileDownLoad",
        type: "POST",
        dataType: "json",
        data: { 'url': url, 'name': name }
    });
}

//加载附件
function loadAttachFiles() {
    $.ajax({
        url: "/Attachment/LoadAttachmentList",
        type: "POST",
        dataType: "json",
        async: false,
        data: {'CaseId':m_Attachment_CaseId, 'billType': m_Attachment_BillType, 'billId': m_Attachment_BillId },
        success: function (req) {
            if (req.IsSuccess) {
                m_Attachments = JSON.parse(req.Data);
                $('#attachFileGrid').datagrid('loadData', m_Attachments);
            }
        },
    });

}

function deleteAttacthmentAction() {

    var row = $('#attachFileGrid').datagrid('getSelected');

    if (row) {

        $.messager.confirm('提示', "是否删除: " + row.Name + " 附件？", function (r) {
            if (r) {
                $.ajax({
                    url: "/Attachment/DeleteAttachment",
                    type: "POST",
                    dataType: "json",
                    async: false,
                    data: { 'attachmentId': row.Id },
                    success: function (req) {
                        if (req.IsSuccess) {
                            $.messager.alert('提示', "删除成功!");
                            loadAttachFiles();
                        } else {
                            $.messager.alert('提示', req.Data);
                        }
                    },
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择附件!");
    }


}


function exitAction() {
    parent.art.dialog.list['dlgAttachment'].close();
}